package com.example.gaming;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.example.ajouconqueror.R;
import com.example.classes.ABuildingInR;
import com.example.classes.IP;
import com.example.classes.UserApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class GameOverActivity extends Activity {
	
	//When game is over, this activity is appeared
	//this activity show the score of each teams and team members.
	int team1point=0;
	int team2point=0;
	int t1m1point=0;
	int t1m2point=0;
	int t2m1point=0;
	int t2m2point=0;
	
	UserApplication UserApp;
	IP ip = new IP();
	private final String HOST = ip.ip;
	private final int PORT = 8080;

	private final String PATH_POST = "/AjouConquerorServer-war/webresources/ForGameOver/returnbuildings";
	
	ArrayList<ABuildingInR> buildings;
	ABuildingInR q;
	
	TextView loser,winner,wtm1p,wtm2p,ltm1p,ltm2p,Wp,Lp,wtm1,wtm2,ltm1,ltm2;
	TextView l, w;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game_over);
		
		buildings = new ArrayList<ABuildingInR>();
		
		UserApp= (UserApplication)getApplication();
		
		gameend t = new gameend();
		t.execute();
		
		l = (TextView) findViewById(R.id.textView1);
		w = (TextView) findViewById(R.id.textView2);
		loser = (TextView) findViewById(R.id.loserTeam);
		winner =(TextView) findViewById(R.id.winnerTeam);
		wtm1 = (TextView) findViewById(R.id.winnerM1);
		wtm2 = (TextView) findViewById(R.id.winnerM2);
		ltm1 = (TextView) findViewById(R.id.loserM1);
		ltm2 = (TextView) findViewById(R.id.loserM2);
		wtm1p = (TextView) findViewById(R.id.winnerM1Point);
		wtm2p = (TextView) findViewById(R.id.winnerM2Point);
		ltm1p = (TextView) findViewById(R.id.loserM1Point);
		ltm2p = (TextView) findViewById(R.id.loserM2Point);
		Wp = (TextView) findViewById(R.id.winnerPoint);
		Lp = (TextView) findViewById(R.id.loserPoint);
	}
    
	
	
	private class gameend extends AsyncTask<String, Void, String> {

		@Override
		protected void onPreExecute() {

			
		}

		@Override
		protected String doInBackground(String... params) {

			String result = "false";

			int tp=UserApp.getRoomid();
			String p=Integer.toString(tp);
			HttpHost target = new HttpHost(HOST, PORT);
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(PATH_POST);

			// set the MIME type for POST message body.
			post.setHeader("Content-Type", "text/plain");

			HttpEntity entity = null;

			StringEntity se;
			
			try {
				se = new StringEntity(p);
				post.setEntity(se); 	

				HttpResponse response = client.execute(target, post);

				// Status code 204 is success with "No content".
				// This happens when a RESTful method returns void.
				if (response.getStatusLine().getStatusCode() < 400) {
					Log.d("TAG", "Error in HTTP GET. Status code: "+response.getStatusLine().getStatusCode());
				} 

				entity = response.getEntity();

				String str = EntityUtils.toString(entity);

				// convert JSON to Person
				result = str;

				Log.i("errorr:", "s "
						+ response.getStatusLine().getStatusCode());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				Log.i("errorr:", "UnsupportedEncodingException");
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				Log.i("errorr:", "ClientProtocolException");
			} catch (IOException e) {
				e.printStackTrace();
				Log.i("errorr:", "IOException");
			}
			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			//result has ArrayList<ABuildingInR>.
			buildings=JSONToBuildingarray(result);
			q = new ABuildingInR();
			for(int i=0; i<buildings.size();i++){
				q=buildings.get(i);
				if(q.getConqueredUId().equals(UserApp.getTeam1_M1())){
					t1m1point+=q.getPoint();
					team1point+=q.getPoint();
				}else if(q.getConqueredUId().equals(UserApp.getTeam1_M2())){
					t1m2point+=q.getPoint();
					team1point+=q.getPoint();
				}else if(q.getConqueredUId().equals(UserApp.getTeam2_M1())){
					t2m1point+=q.getPoint();
					team2point+=q.getPoint();
				}else if(q.getConqueredUId().equals(UserApp.getTeam2_M2())){
					t2m2point+=q.getPoint();
					team2point+=q.getPoint();
				}
			}
			
			//TextView loser,winner,wtm1p,wtm2p,ltm1p,ltm2p,Wp,Lp,wtm1,wtm2,ltm1,ltm2;	
			
		if(team1point>team2point){
			loser.setText("Team2");
			winner.setText("Team1");
			wtm1.setText(UserApp.getTeam1_M1());
			if(UserApp.getTeam1_M2()==null){
			wtm2.setText("");
			}else{
			wtm2.setText(UserApp.getTeam1_M2());
			}
			if(UserApp.getTeam2_M1()==null){
			ltm1.setText("");
			}else{
			ltm1.setText(UserApp.getTeam2_M1());
			}
			if(UserApp.getTeam2_M2()==null){
			ltm2.setText("");
			}else{
			ltm2.setText(UserApp.getTeam2_M2());
			}
			Wp.setText(""+team1point);
			Lp.setText(""+team2point);
			wtm1p.setText(""+t1m1point);
			wtm2p.setText(""+t1m2point);
			ltm1p.setText(""+t2m1point);
			ltm2p.setText(""+t2m2point);
			
		}else if(team2point>team1point){
			loser.setText("Team1");
			winner.setText("Team2");
			ltm1.setText(UserApp.getTeam1_M1());
			if(UserApp.getTeam1_M2()==null){
			ltm2.setText("");
			}else{
			ltm2.setText(UserApp.getTeam1_M2());
			}
			if(UserApp.getTeam2_M1()==null){
			wtm1.setText(""); 
			}else{
			wtm1.setText(UserApp.getTeam2_M1());
			}
			if(UserApp.getTeam2_M2()==null){
			wtm2.setText("");			 
			}else{
		    wtm2.setText(UserApp.getTeam2_M2());
			}
			Lp.setText(""+team1point);
			Wp.setText(""+team2point);
			ltm1p.setText(""+t1m1point);
			ltm2p.setText(""+t1m2point);
			wtm1p.setText(""+t2m1point);
			wtm2p.setText(""+t2m2point);
		} else{
			l.setText("");
			w.setText("Draw!");
			loser.setText("Team1");
			winner.setText("Team2");
			ltm1.setText(UserApp.getTeam1_M1());
			if(UserApp.getTeam1_M2()==null){
			ltm2.setText("");
			}else{
			ltm2.setText(UserApp.getTeam1_M2());
			}
			if(UserApp.getTeam2_M1()==null){
			wtm1.setText(""); 
			}else{
			wtm1.setText(UserApp.getTeam2_M1());
			}
			if(UserApp.getTeam2_M2()==null){
			wtm2.setText("");			 
			}else{
		    wtm2.setText(UserApp.getTeam2_M2());
			}
			Lp.setText(""+team1point);
			Wp.setText(""+team2point);
			ltm1p.setText(""+t1m1point);
			ltm2p.setText(""+t1m2point);
			wtm1p.setText(""+t2m1point);
			wtm2p.setText(""+t2m2point);
			
		}
			
			
			
			
			
		}
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game_over, menu);
		return true;
	}

	private ArrayList<ABuildingInR> JSONToBuildingarray(String json) {
		Log.d("gg", json);
		Gson gson = new Gson();

		Type t = new TypeToken<ArrayList<ABuildingInR>>() {
		}.getType();

		ArrayList<ABuildingInR> p = gson.fromJson(json, t);

		return p;
	}
	
}
